package org.iabako.server.serviceimpl.business;

import org.iabako.server.dao.JpaDAO;
import org.iabako.server.dao.business.CustomFieldDAO;
import org.iabako.shared.entity.business.CustomField;
import org.iabako.shared.entity.business.CustomFieldListItem;
import org.iabako.shared.entity.business.CustomFieldValue;
import org.iabako.shared.entity.enumeration.CustomFieldType;
import org.iabako.shared.iservice.business.CustomFieldService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by jose on 22/09/14.
 */

@Service("customFieldService")
public class CustomFieldServiceImpl extends AbstractServiceImpl implements CustomFieldService {

    @Autowired
    private CustomFieldDAO customFieldDAO;

    @Override
    protected JpaDAO getDAO() {
        return customFieldDAO;
    }

    public List<CustomFieldListItem> getCustomFieldsListItemValues(CustomField customField) {
        return customFieldDAO.getCustomFieldsItemValues(customField);
    }

    public List<CustomField> getCustomFields() {
        return customFieldDAO.getCustomFields();
    }

    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public void saveCustomFields(List<CustomField> fields, Map<CustomField, List<String>> customFieldListItemTextAdded,
                                 Map<CustomField, List<String>> customFieldListItemTextDeleted) {

        List<CustomField> customFieldsDB = getCustomFields();

        //persist and merge CustomFields
        for (CustomField field : fields) {

            if (field.getCustomFieldType() == CustomFieldType.list
                    && customFieldListItemTextAdded.get(field) != null) {

                //persist CustomFieldsListItem
                for (String itemText : customFieldListItemTextAdded.get(field)) {
                    CustomFieldListItem item = new CustomFieldListItem();
                    item.setLabel(itemText);
                    item.setCustomField(field);
                    //for cascading :
                    field.getCustomFieldListItems().add(item);
                }
            }

            field.setEnterprise(getSessionUser().getEnterprise());
            super.save(field);
        }

        //delete customFields
        for (CustomField customFieldDB : customFieldsDB) {
            if (!fields.contains(customFieldDB)) {
                super.remove(customFieldDB);
            }
        }

        //delete customFieldListItems
        customFieldDAO.removeCustomListItemByLabels(customFieldListItemTextDeleted);
    }

    public Long getUsagesCustomField(CustomField customField) {
        if (customField.getId() == null) {
            return 0L;
        }
        return customFieldDAO.getUsagesCustomField(customField);
    }

    public Long getUsagesCustomFieldListValue(CustomField customField, String itemText) {
        if (customField.getId() == null) {
            return 0L;
        }
        return customFieldDAO.getUsagesCustomFieldItemValue(customField, itemText);
    }

    public List<CustomFieldValue> getCustomCriteriaSearchFields() {
        List<CustomFieldValue> values = new ArrayList<CustomFieldValue>();
        List<CustomField> customFields = customFieldDAO.getCustomCriteriaSearchFields(getSessionUser().getUserCredit().getIabakoPackage().getCustomFieldLimit());
        for (CustomField field : customFields) {
            CustomFieldValue value = new CustomFieldValue();
            value.setCustomField(field);
            values.add(value);
        }
        return values;
    }
}
